package com.jl.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jl.commons.result.PageDataInfo;
import com.jl.mapper.KeyWordMapper;
import com.jl.model.KeyWord;
import com.jl.model.in.KeyWordIn;
import com.jl.service.IKeyWordService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 故障关键词表 服务实现类
 * </p>
 *
 * @author JiangJian
 * @since 2019-03-06
 */
@Service
public class KeyWordServiceImpl extends ServiceImpl<KeyWordMapper, KeyWord> implements IKeyWordService {

    @Override
    public void selectKeyWordDataGrid(PageDataInfo<Map<String, Object>> pageDataInfo, QueryWrapper<KeyWord> qw) {
        Page<Map<String,Object>> page = new Page<>();
        page.setSize(pageDataInfo.getSize());
        page.setCurrent(pageDataInfo.getCurrent());
        List<Map<String,Object>> mapList =baseMapper.selectDataGrid(page,qw);
        pageDataInfo.setTotal(page.getTotal());
        pageDataInfo.setRecords(mapList);
    }

    @Override
    public boolean isKeyWordUsed(String name) {
        return baseMapper.selectKeyWordNameCount(name)>0;
    }

    @Override
    public boolean isKeyWordCanUse(String name, Long keyWordId) {
        KeyWordIn keyWordIn = baseMapper.selectKeyWordInByName(name);
        if(keyWordIn == null){
            //如果找不到数据则表示该数据没有被使用
            return true;
        }else{
            if(keyWordIn.getId().equals(keyWordIn)){
                //如果这个数据就是他本身则可以使用
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean addKeyWordBindCount(Long id) {
        return baseMapper.updateKeyWordBindCount(id,1) >0;
    }

    @Override
    public void selectTopKeyWord(PageDataInfo<Map<String, Object>> pageDataInfo, Integer top, String startTime, String endTime) {
        List<Map<String,Object>> mapList = baseMapper.selectTopKeyWord(top,startTime,endTime);
        pageDataInfo.setTotal(mapList.size());
        pageDataInfo.setRecords(mapList);
    }
}
